<?php
/**
 * Created by PhpStorm.
 * User: zjx
 * Date: 2016/11/25
 * Time: 10:32
 */

namespace App\Services\Utils;
use App\Http\Models\ColorBeautyContactLen;
use App\Http\Models\ColorGlass;
use App\Http\Models\ColorLen;
use App\Http\Models\Product;
use App\Http\Models\Products\Beauty\ColorBeautyPivot;
use App\Http\Models\Products\Glass\ColorGlassesPivot;
use App\Http\Models\Products\Lens\ColorLensPivot;

//use Illuminate\Database\Eloquent\Builder; ?

/**
 * 获取多态商品的属性
 * Class MorphProduct
 * @package App\Services\Utils
 */
trait MorphProduct
{
    public function getSkuCode($product_id,$color_id)
    {
        $product =  Product::find($product_id);

        if ( !$product ) return '当前无商品';

        if ( $color_id==null )
        {
            return self::checkObject($product->productable->code);
        }

        if ( $product->productable_type == 'Glass' )
        {

            return self::checkObject(
                ColorGlassesPivot::where([
                    ['product_id',$product->productable->id],
                    ['color_id',$color_id],
                ])->first()
            );
        }elseif ( $product->productable_type == 'BeautyContactLens' )
        {
            return self::checkObject(ColorBeautyPivot::where([
                ['product_id',$product->productable->id],
                ['color_id',$color_id],
            ])->first()
            );
        }elseif ( $product->productable_type == 'Lens' )
        {
            return self::checkObject(ColorLensPivot::where([
                ['product_id',$product->productable->id],
                ['color_id',$color_id],
            ])->first()
            );
        }
        else{
            return self::checkObject($product->productable->code);
        }
    }

    public function checkObject($object)
    {
        if( is_string($object) )
        {
            return isset($object)?$object:'未知';
        }

        if (  !$object )
        {
            return '未知';
        }else{
            return $object->sku_code;
        }

    }

    public function getMorphProduct($productable_id,$productable_type)
    {
        $product = Product::where([
            ['productable_id',$productable_id],
            ['productable_type',$productable_type]
        ])->first();

        return @$product;
    }

    /**
     * 根据商品类型，返回颜色信息
     * @param $product_id
     * @param $color_id
     * @return array
     */
    public function getColorInfo($product_id, $color_id)
    {
        $product = Product::find($product_id);
        switch ( $product->productable_type )
        {
            case 'Glass' :

                $color_img = ColorGlassesPivot::where([
                    ['product_id',$product->productable->id],
                    ['color_id',$color_id]
                ])->first()->color_img;

                $color_name = ColorGlass::find($color_id)->color;

                return ['color_img'=>$color_img,'color_name'=>$color_name];

                break;
            case  'Lens' :
                $color_img = ColorLensPivot::where([
                    ['product_id',$product->productable->id],
                    ['color_id',$color_id]
                ])->first()->color_img;

                $color_name = ColorLen::find($color_id)->color;

                return ['color_img'=>$color_img,'color_name'=>$color_name];
                break;
            case 'BeautyContactLens' :
                $color_img = ColorBeautyPivot::where([
                    ['product_id',$product->productable->id],
                    ['color_id',$color_id]
                ])->first()->color_img;

                $color_name = ColorBeautyContactLen::find($color_id)->color;

                return ['color_img'=>$color_img,'color_name'=>$color_name];
                break;
            default :
                return ['color_img'=>'','color_name'=>''];
        }

    }
    
}